from django.db import models
from valar.models.core import VModel


class LubricatingGreaseBatch(VModel):
    """润滑脂生产批次模型"""
    
    # 批次基础信息 (Base Batch Information)
    batch_name = models.CharField(max_length=255, verbose_name="批次名称", unique=True, null=True, blank=True)
    product_mass_kg = models.FloatField(verbose_name="产品质量（kg）", help_text="来自表1", null=True, blank=True)

    # 表1：原料投入量 (Table 1: Raw Material Input)
    calcium_petroleum_sulfonate_kg = models.FloatField(verbose_name="石油磺酸钙投入量（kg）", null=True, blank=True)
    neutral_oil_kg = models.FloatField(verbose_name="中性油投入量（kg）", null=True, blank=True)
    calcium_hydroxide_kg = models.FloatField(verbose_name="氢氧化钙投入量（kg）", null=True, blank=True)
    boric_acid_kg = models.FloatField(verbose_name="硼酸投入量（kg）", null=True, blank=True)
    hydroxy_stearic_acid_kg = models.FloatField(verbose_name="12羟基硬脂酸投入量（kg）", null=True, blank=True)
    acetic_acid_kg = models.FloatField(verbose_name="醋酸投入量（kg）", null=True, blank=True)
    ga_034_kg = models.FloatField(verbose_name="GA-034投入量（kg）", null=True, blank=True)

    # 表2：能耗消耗量 (Table 2: Energy Consumption)
    natural_gas_m3 = models.FloatField(verbose_name="天然气消耗量（m³）", null=True, blank=True)
    electricity_kwh = models.FloatField(verbose_name="电消耗量（kWh）", null=True, blank=True)
    steam_t = models.FloatField(verbose_name="0.35MPa蒸汽消耗量（t）", null=True, blank=True)

    # 辅助材料（水）消耗量 (Auxiliary Material (Water) Consumption)
    water_kg = models.FloatField(verbose_name="水消耗量（kg）", help_text="来自辅助材料带入排放计算", null=True, blank=True)

    # 表3 & 表8：详细运输数据 (Table 3 & 8: Detailed Transportation Data)
    # 厂外原料运输 (External Raw Material Transport)
    calcium_petroleum_sulfonate_transport_km = models.FloatField(verbose_name="石油磺酸钙行驶里程（km）", null=True, blank=True)
    calcium_petroleum_sulfonate_transport_emission_kgCO2 = models.FloatField(verbose_name="石油磺酸钙运输排放（kgCO₂）", null=True, blank=True)

    neutral_oil_transport_km = models.FloatField(verbose_name="中性油行驶里程（km）", null=True, blank=True)
    neutral_oil_transport_emission_kgCO2 = models.FloatField(verbose_name="中性油运输排放（kgCO₂）", null=True, blank=True)

    calcium_hydroxide_transport_km = models.FloatField(verbose_name="氢氧化钙行驶里程（km）", null=True, blank=True)
    calcium_hydroxide_transport_emission_kgCO2 = models.FloatField(verbose_name="氢氧化钙运输排放（kgCO₂）", null=True, blank=True)

    boric_acid_transport_km = models.FloatField(verbose_name="硼酸行驶里程（km）", null=True, blank=True)
    boric_acid_transport_emission_kgCO2 = models.FloatField(verbose_name="硼酸运输排放（kgCO₂）", null=True, blank=True)

    hydroxy_stearic_acid_transport_km = models.FloatField(verbose_name="12羟基硬脂酸行驶里程（km）", null=True, blank=True)
    hydroxy_stearic_acid_transport_emission_kgCO2 = models.FloatField(verbose_name="12羟基硬脂酸运输排放（kgCO₂）", null=True, blank=True)

    acetic_acid_transport_km = models.FloatField(verbose_name="醋酸行驶里程（km）", null=True, blank=True)
    acetic_acid_transport_emission_kgCO2 = models.FloatField(verbose_name="醋酸运输排放（kgCO₂）", null=True, blank=True)

    ga_034_transport_km = models.FloatField(verbose_name="GA-034行驶里程（km）", null=True, blank=True)
    ga_034_transport_emission_kgCO2 = models.FloatField(verbose_name="GA-034运输排放（kgCO₂）", null=True, blank=True)

    # 厂内运输 (Internal Transport)
    internal_transport_emission_kgCO2 = models.FloatField(verbose_name="厂内运输排放（kgCO₂）", help_text="文档中直接给出的电叉车排放量", null=True, blank=True)

    # 厂外产品运输 (External Product Transport)
    external_product_transport_km = models.FloatField(verbose_name="厂外产品运输里程（km）", help_text="文档中加权值1245.58km", null=True, blank=True)
    external_product_transport_emission_kgCO2 = models.FloatField(verbose_name="厂外产品运输排放（kgCO₂）", null=True, blank=True)

    # 汇总计算结果 (Summary of Calculated Results from Table 9 & Table 11)
    raw_material_emission_total_kgCO2 = models.FloatField(verbose_name="原料带入排放总量（kgCO₂）", null=True, blank=True)
    energy_emission_total_kgCO2 = models.FloatField(verbose_name="能耗排放总量（kgCO₂）", null=True, blank=True)
    auxiliary_emission_total_kgCO2 = models.FloatField(verbose_name="辅助材料带入排放总量（kgCO₂）", null=True, blank=True)
    transport_emission_total_kgCO2 = models.FloatField(verbose_name="运输过程排放总量（kgCO₂）", null=True, blank=True)
    total_emission_kgCO2 = models.FloatField(verbose_name="合计排放量（kgCO₂）", null=True, blank=True)
    carbon_footprint_kgCO2_per_t = models.FloatField(verbose_name="产品碳足迹（kgCO₂/t）", null=True, blank=True)
    
    # 详细结果分析 (Detailed Result Analysis - 从CarbonFootprintResult模型合并)
    raw_material_stage_kgCO2 = models.FloatField(verbose_name="原料带入（kgCO₂/t）", null=True, blank=True)
    energy_consumption_stage_kgCO2 = models.FloatField(verbose_name="能源消耗（kgCO₂/t）", null=True, blank=True)
    auxiliary_material_stage_kgCO2 = models.FloatField(verbose_name="辅助材料带入（kgCO₂/t）", null=True, blank=True)
    transport_stage_kgCO2 = models.FloatField(verbose_name="运输过程（kgCO₂/t）", null=True, blank=True)
    
    raw_material_stage_percentage = models.FloatField(verbose_name="原料带入占比（%）", null=True, blank=True)
    energy_consumption_stage_percentage = models.FloatField(verbose_name="能源消耗占比（%）", null=True, blank=True)
    auxiliary_material_stage_percentage = models.FloatField(verbose_name="辅助材料带入占比（%）", null=True, blank=True)
    transport_stage_percentage = models.FloatField(verbose_name="运输过程占比（%）", null=True, blank=True)
    
    class Meta:
        verbose_name = '润滑脂生产批次'
        verbose_name_plural = '润滑脂生产批次'

    def __str__(self):
        return self.batch_name


